<template>
    <el-button type="primary" size="small" @click="AddTracebility()">添加</el-button>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column type="index" width="55" label="序号" />
      <el-table-column prop="shoppName" label="商品名称" />
      <el-table-column prop="proBatch" label="生产批次" />
      <el-table-column prop="shelfLife" label="保质期" />
      <el-table-column prop="proLicense" label="生产许可证" />
      <el-table-column prop="manufacturers" label="生产企业" />
      <el-table-column label="信息完善度" />
      <el-table-column label="操作" >
        <template v-slot="scope">
          <el-button type="primary" size="small" @click="editapp()">编辑</el-button>
          <el-button type="success" size="small" @click="deleteapp()">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <el-dialog v-model="table1" title="Shipping address" width="500">
    <el-form :model="form">
      <el-form-item label="溯源编号" >
        <el-input v-model="form.traceabilityId" autocomplete="off" />
      </el-form-item>
      <el-form-item label="商品名称" >
        <el-select v-model="form.shoppId">
        <el-option v-for="item in shopps" :label="item.shoppName" :value="item.shoppId" />
      </el-select>
      </el-form-item>
      <el-form-item label="生产批次" >
        <el-input v-model="form.proBatch" autocomplete="off" />
      </el-form-item>
      <el-form-item label="保质期（天）" >
        <el-input v-model="form.shelfLife" autocomplete="off" />
      </el-form-item>
      <el-form-item label="生产许可证" >
        <el-input v-model="form.proLicense" autocomplete="off" />
      </el-form-item>
      <el-form-item label="生产企业" >
        <el-input v-model="form.manufacturers" autocomplete="off" />
      </el-form-item> 
      <el-form-item label="封面图" >
        <el-upload
    class="avatar-uploader"
    action="https://localhost:7227/api/Management/UploadFile"
    :show-file-list="false"
    :on-success="handleAvatarSuccess"
    :before-upload="beforeAvatarUpload"
  >
    <img v-if="form.coverPic" :src="form.coverPic" class="avatar" />
    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
  </el-upload>
      </el-form-item> 
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="addapp()">
          添加
        </el-button>
      </div>
    </template>
  </el-dialog>


  </template>
  
  <script lang="ts" setup>
  import { ref,onMounted,reactive } from 'vue'
  import axios from 'axios'
  import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

import type { UploadProps } from 'element-plus'
  const tableData = ref([])

  onMounted(()=>{
    getlist();
    shoppslist();
  })


  const shopps=ref<any>([]);
  const shoppslist=()=>{
    axios.get('https://localhost:7227/api/Shopp/GetShopp').then(res=>{
      shopps.value=res.data.data;
    })
  }


  const getlist=()=>{
    axios.get('https://localhost:7227/api/Tracebility/GetTracebility').then(res=>{
      tableData.value = res.data.data
    })
  }

  const table1=ref(false);

  const AddTracebility=()=>{
    table1.value=true;
  }

  const form = reactive({
  id:0,
  traceabilityId: '',
  shoppId: null,
  proBatch: '',
  shelfLife: null,
  proLicense: '',
  manufacturers: '',
  coverPic: 'sdfasdfa',
  isDelete: false,
})

const handleAvatarSuccess: UploadProps['onSuccess'] = (
  response,
) => {
  form.coverPic = response
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  return true
}

const addapp=()=>{
  axios.post('https://localhost:7227/api/Tracebility/AddTracebility',form).then(res=>{
    console.log(res)
    if(res.data.statusCode==0){
      ElMessage.success('添加成功')
      table1.value=false;
      getlist();
    }else{
      ElMessage.error('添加失败')
    }
  })
}

  </script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
  